package env_service

import (
	"beta/models"
	"beta/pkg/logger"
	"errors"
)

type EnvService struct {
	TraceId string
}

func (e EnvService) GetOneAppEnvWithNamespace(appName, namespace string) (*models.AppEnv, error) {
	queryMap := e.getMaps()
	queryMap["app_name"] = appName
	queryMap["app_namespace"] = namespace

	env, err := models.GetEnv(queryMap)
	logger.ZapLogger.Infow("获取环境信息", "env", env, "traceId", e.TraceId)
	if err != nil {
		return nil, err
	}

	return env, nil
}

func (e EnvService) GetAllAppEnvsWithNamespace(namespace string) (map[string]*models.AppEnv, error) {
	queryMap := e.getMaps()
	queryMap["app_namespace"] = namespace

	envs, err := models.GetEnvs(queryMap)
	logger.ZapLogger.Infow("获取环境信息", "envs", envs, "traceId", e.TraceId)
	if err != nil {
		return nil, err
	}

	if len(envs) <= 0 {
		return nil, errors.New("数据不能为空")
	}

	data := make(map[string]*models.AppEnv)
	for _, v := range envs {
		data[v.AppName] = v
	}

	return data, nil

}

func (e EnvService) getMaps() map[string]interface{} {

	maps := make(map[string]interface{})
	maps["is_deleted"] = 0

	return maps
}